package hashMap.day01;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

public class HashMap01 {


    @Test
    public void testHashMap01() {
        System.out.println("this is a first testing...");
        //定义一个数组
        String[] arr = {"A", "B", "C", "D"};
        //利用随机数来投票
        ArrayList<String> lists = new ArrayList<>();

        Random random = new Random();
        for (int i = 0; i < 80; i++) {
            //生成随机数
            int num = random.nextInt(arr.length);
//            System.out.println(num);
            lists.add(arr[num]);
        }
        System.out.println(lists);

        //统计票数
        HashMap<String, Integer> hashMap = new HashMap();

        for (String s : lists) {
            if (hashMap.containsKey(s)) {
                //取出票数
                int count = hashMap.get(s);
                hashMap.put(s,++count);
            } else {
                hashMap.put(s, 1);

            }
        }

        System.out.println(hashMap);

        //获取最大值
        int max = 0;
        for(String s: hashMap.keySet()) {
            if(max < hashMap.get(s)) {
                max = hashMap.get(s);
            }
        }
        System.out.println("最大值: "+max);
    }
}
